<script setup lang="ts">
import { ref } from 'vue'
import CustomNavbar from './components/CustomNavbar.vue'
import CategroryPanel from './components/CategroryPanel.vue'
import { getBannerAPI, getCategroyListAPI, getHotMutliAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem, CategoryItem, HotMutli } from '@/types/home'
import HotPanle from './components/HotPanle.vue'
import type { XtxGuessInstance } from '@/types/component'
import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'
//获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getBannerList = async () => {
  const res = await getBannerAPI()
  bannerList.value = res.result
}

//获取前台分类数据
const categoryList = ref<CategoryItem[]>([])
const getCategroy = async () => {
  const res = await getCategroyListAPI()
  categoryList.value = res.result
}

//获取热门推荐数据
const hotmutliList = ref<HotMutli[]>([])
const getHotMutliList = async () => {
  const res = await getHotMutliAPI()
  console.log(res)
  hotmutliList.value = res.result
}

const { guess, onScrolltolower } = useGuessList()
const isTrigger = ref(false)
const onRefresh = async () => {
  isTrigger.value = true
  guess.value?.resetData()
  await Promise.all([getBannerList(), getHotMutliList(), getCategroy()])
  isTrigger.value = false
}
const loading = ref(false)
onLoad(async () => {
  loading.value = true
  await Promise.all([getBannerList(), getCategroy(), getHotMutliList()])
  loading.value = false
})
</script>

<template>
  <CustomNavbar />
  <scroll-view
    refresher-enabled
    :refresher-triggered="isTrigger"
    @refresherrefresh="onRefresh"
    @scrolltolower="onScrolltolower"
    class="scroll-view"
    scroll-y
  >
    <PageSkeleton v-if="loading" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategroryPanel :categorylist="categoryList" />
      <HotPanle :list="hotmutliList" />
      <XtxGuess ref="guess" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
